// pages/search/search.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shoopingarray: [    ],
    all:[],
    shoopingtext: '', //搜索框的值
    history: false, //显示历史记录
    noneview: false, //显示未找到提示
    shoppinglist: false, //显示商品列表
    historyArray: [], //历史记录数组,
    newArray: [], //添加历史记录数组
    nArray:[],
    pingjiayu: ['店长推荐', '火热预约', '评价极高', '全新服务'],
  },


  cancelTap() {
    wx.switchTab({
      url: '../home/home',
    })
  },

  onBindFocus() {

  },

  //清除历史记录
  cleanhistory: function (e) {
    this.setData({
      history: false, //隐藏历史记录
      historyArray: [], //清空历史记录数组
      shoopingtext: "", //清空搜索框
      nArray:[]
    })
    wx.setStorageSync('newArray', [])
  },

  //搜索
  onBindConfirm() {
    if(this.data.shoopingtext == ''){
      wx.showToast({
        title: '输入为空哦',
        icon:'none',
        duration:500
      })
    }else{
      var searchtext = this.data.shoopingtext; //搜索框的值
      var item = wx.getStorageSync('newArray')
      var index = 0
      for(var i =  0 ; i<item.length ; i++){
        if(item[i] == this.data.shoopingtext){
          index = 1
        }
      }
      if(index == 0){
        item.push(this.data.shoopingtext)
        wx.setStorageSync('newArray', item)
        this.setData({
          nArray:item
        })
      }
     
     
      var sss = true;
      if (searchtext != "") {
        //将搜索框的值赋给历史数组
        this.data.historyArray.push(searchtext);
        //模糊查询 循环查询数组中的name字段
        for (var index in this.data.shoopingarray) {
          console.log(this.data.shoopingarray[index].name)
          var num = this.data.shoopingarray[index].name.indexOf(searchtext);
          let temp = 'shoopingarray[' + index + '].status';
          if (num != -1) { //不匹配的不显示
            this.setData({
              [temp]: 1,
            })
            sss = false //隐藏未找到提示
          }
        }
        this.setData({
          history: false, //隐藏历史记录
          noneview: sss, //隐藏未找到提示
          shoppinglist: true, //显示商品列表
          newArray: this.data.historyArray //给新历史记录数组赋值
        })
       
  
        // wx.setStorageSync('newArray', wx.getStorageSync('newArray').push(this.data.newArray[this.data.newArray.length-1]))
      } else {
        this.setData({
          noneview: true, //显示未找到提示
          shoppinglist: false, //隐藏商品列表
          history: false, //隐藏历史记录
        })
      }
    }
   
  },

  //搜索框的值
  shoppinginput(e) {
    //当删除input的值为空时
    console.log(e.detail.value)
    if (e.detail.value == "") {
      this.setData({
        history: true, //显示历史记录
        shoppinglist: false //隐藏商品列表
      });
      //所有商品列表的状态改为0
      for (var index in this.data.shoopingarray) {
        let temp = 'shoopingarray[' + index + '].status';
        this.setData({
          [temp]: 0,
        })
      }
    }
    this.setData({
      shoopingtext: e.detail.value
    })
  },

  textfz: function (e) {
    this.setData({
      shoopingtext: e.target.dataset.text
    })
    this.onBindConfirm()
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      key:app.globalData.key
    })
    if(wx.getStorageSync('newArray')!=''){
      this.setData({
        nArray:wx.getStorageSync('newArray'),
        history:true
      })
    }else{
        wx.setStorageSync('newArray', [])
    }
    var product = app.globalData.product
    var items = []
    console.log(product)
    for (var i = 0; i < app.globalData.product_sum; i++) {
      var item = {
        img: product[i].product_swiper_pic1,
        name: product[i].product_name,
        price: product[i].cost,
        id: i.toString(),
        pre_price: product[i].pre_cost,
        product_ID: product[i].product_code,
        a: 0,
        product_introduce:product[i].product_introduce
      }
      items.push(item)
    }
    var service = app.globalData.service
    for (var i = 0; i < app.globalData.service_sum; i++) {
      var item = {
        img: service[i].service_img,
        name: service[i].service_name,
        id: i.toString(),
        introduce:service[i].service_introduce,
        service_ID: service[i].service_name,
        phone:service[i].service_phone,
        a: 1
      }
      items.push(item)
    }
    var service_items = app.globalData.service_items
    for (var i = 0; i < app.globalData.service_items_sum; i++) {
      var item = {
        img: service_items[i].item_pic,
        name: service_items[i].item_name,
        price: service_items[i].actual_price,
        id: i.toString(),
        pre_price: service_items[i].all_price,
        service_items_ID:service_items[i].service_name,
        a: 2
      }
      items.push(item)
    }
    console.log(items)
    this.setData({
      shoopingarray: items,
      all : items
    })
    console.log(this.data.shoopingarray)
  },
  shangpinDetail(e) {
    console.log(e.currentTarget.dataset)
    var postId = e.currentTarget.dataset.postid
    setTimeout(function (e) {
      wx.navigateTo({
        url: '../post-detail/post-detail?id=' + postId
      })
    }, 200)

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  GoDetail: function (e) {
    var that = this
    var index = e.currentTarget.dataset.postid
    console.log(index)

    console.log(that.data.shoopingarray)
    if (that.data.all[index].a == 0) {
      console.log(0)
      console.log(that.data.all[index].product_ID)
      that.GoProductDetail(that.data.all[index].product_ID)
    }

    else if (that.data.all[index].a == 1) {
      console.log(1)
      that.GoServiceDetail(that.data.all[index].name)
    }

    else if (that.data.all[index].a == 2) {
      console.log(2)
      that.GoServiceDetail(that.data.all[index].name)   
    }
  },
  GoProductDetail(id) {
    var that = this
    console.log(id)
    setTimeout(function (e) {
      app.navigateToDeTail(id)
    }, 500)
  },
  GoServiceDetail(id) {
    var that = this
    console.log(id)
    // console.log(this.data.service[index].service_name)
    setTimeout(function (e) {
      app.navigateToService(id)
    }, 500)
  },
  // GoService_itemsDetail(id){
    
  // }
})